/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.models.impl;

import com.ntb.common.DatabaseConnection;
import com.ntb.models.PermissionsDAO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DATBP
 */
public class PermissionsDAOImpl extends DatabaseConnection implements PermissionsDAO {

    private List permissionsTable;

    public PermissionsDAOImpl() {
        /**
         *  create all permissions
         */
        permissionsTable = new ArrayList();

        // Lands
        permissionsTable.add(new String[] {"location", "Locations", "land"});
        permissionsTable.add(new String[] {"costHistory", "Cost Histories", "land"});
        permissionsTable.add(new String[] {"landPlan", "Land Plans Laid", "land"});
        permissionsTable.add(new String[] {"land", "Manage Lands", "land"});

        // Buildings
        permissionsTable.add(new String[]{"buildingType", "Building Types", "building"});
        permissionsTable.add(new String[]{"building", "Manage Buildings", "building"});

        // Sales
        permissionsTable.add(new String[]{"stamp", "SD & Registration", "sale"});
        permissionsTable.add(new String[]{"flat", "Flats (Houses, Shops)", "sale"});
        permissionsTable.add(new String[]{"sale", "Manage Sales", "sale"});

        // Users
        permissionsTable.add(new String[]{"user", "Manage Users", "user"});
        permissionsTable.add(new String[]{"permission", "Permissions", "user"});
        permissionsTable.add(new String[]{"group", "Groups", "user"});

        // Contacts
        permissionsTable.add(new String[]{"contact", "Contacts", "contact"});
    }

    /**
     * used to get all permissions to display
     * 
     * @return permissionTable
     */
    public Iterator getPermissionsTable() {
        return permissionsTable.iterator();
    }

    /**
     * used to get permissions of specific group
     * 
     * @param groupId
     * @return permissionList
     */
    public List<String> getPermissionsList(int groupId) {
        if (groupId == 1) {
            return this.getAllPermissions();
        }
        List<String> list = new ArrayList<String>();
        try {
            openConnection();
            ResultSet rs = getResultset("select Module from Permissions where GroupID = " + groupId);
            while (rs.next()) {
                list.add(rs.getString("Module"));
            }
            closeConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }
    
    /**
     * used to update permissions
     * 
     * @param permissions
     * @param groupId
     * @return status
     */
    public int updatePermissions(String[] permissions, int groupId) {
        boolean success = false;
        try {
            openConnection();
            conn.setAutoCommit(false);
            String delete = "Delete Permissions where GroupID=?";
            PreparedStatement pst = conn.prepareStatement(delete);
            pst.setInt(1, groupId);

            pst.executeUpdate();
            String add = "insert Permissions values(?,?)";
            for (String module : permissions) {
                pst = conn.prepareStatement(add);
                pst.setInt(1, groupId);
                pst.setString(2, module);
                pst.executeUpdate();
            }
            success = true;
            conn.commit();
        } catch (SQLException ex) {
            try {
                conn.rollback();
                Logger.getLogger(PermissionsDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex1) {
                Logger.getLogger(PermissionsDAOImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        closeConnection();
        if (success) {
            return 1;
        }
        return -1;
    }

    /**
     * used to get all permissions
     * 
     * @return allPermissions
     */
    public List<String> getAllPermissions() {
        List<String> list = new ArrayList<String>();
        for (Iterator <String[]> it = permissionsTable.iterator(); it.hasNext();) {
            list.add(it.next()[0]);
        }
        return list;
    }
}
